#!/usr/bin/env python
# -*- coding:utf-8 -*-
import argparse
import sys

sys.path.append('..')
from common_utils import CBBCommonUtils


def get_plugin_info():
    plugin_info = {
        "name": "centos_system_001 Confirm_the_current_system_account_and_system_version",
        "plugin_id": "centos_system_001",
        "plugin_type": "System",
        "info": "Confirm whether the current system account and system versions are the entered values.",
        "level": "C",
        "module": "Safety reinforcement",
        "author": "congshz",
        "keyword": "Safety reinforcement",
        "configable": "true",
        "config": "current_username, system_version",
        "current_username": "root",
        "system_version": "CentOS 7"
    }
    return plugin_info


logger = None
cur_user = None
cur_module = None
cur_task = None


def set_plugin_logger(setter, user, module, task, *args, **kwargs):
    global logger, cur_user, cur_module, cur_task
    logger = setter
    cur_user = user
    cur_module = module
    cur_task = task


# 扫描函数
def scan(ip, sys_user, sys_pwd, current_username, system_version):
    des_list = []
    error_count = 0
    username_cmd = "whoami"
    result, output = CBBCommonUtils.cbb_run_cmd(ip, username_cmd, sys_user, sys_pwd)
    if not result and len(output) != 0:
        if output[0].strip("\n") == current_username:
            des = "The current username IS the username entered."
        else:
            des = "The current username is NOT the username entered."
            error_count += 1
    else:
        des = "CMD process ERROR."
        error_count += 1
    des_list.append(des)

    if system_version == "" or system_version == " ":
        des = "No system version entered."
        des_list.append(des)
        return des_list, error_count

    system_version_cmd = "cat /etc/redhat-release"
    result, output = CBBCommonUtils.cbb_run_cmd(ip, system_version_cmd, sys_user, sys_pwd)
    if not result and len(output) != 0:
        system_version_part = system_version.split()
        error_version = 0
        for item in system_version_part:
            if item not in output[0]:
                error_version += 1
                break
        if error_version == 0:
            des = "Operation system version IS the version entered."
        else:
            des = "Operation system version is NOT the version entered."
            error_count += 1
    else:
        des = "CMD process ERROR."
        error_count += 1
    des_list.append(des)
    return des_list, error_count


def check(ip, *args, **kwargs):
    sys_user = kwargs.get("system_user")
    sys_pwd = kwargs.get("system_pwd")
    comm = kwargs.get("command")
    current_username = 'root'
    system_version = 'CentOS 7'
    try:
        des_list = []
        get_plugin_info()
        if comm == 1 or comm == 2 or comm == 3:
            des_scan, error_scan = scan(ip, sys_user, sys_pwd, current_username, system_version)
            des_list.extend(des_scan)
            step_error = int(error_scan)
        else:
            get_plugin_info()
            step_error = 0
        code = 0 if step_error == 0 else 1
        return {"code": code, "count": step_error, "des": des_list}
    except Exception as er:
        code = 1
        result = {"code": code, "count": 0, "des": ["ERROR:", str(er)]}
        return {"code": code, "count": 0, "des": result}


# if __name__ == "__main__":
#     parser = argparse.ArgumentParser(description='Prevent users from interactive login.')
#     parser.add_argument("-i", type=str, default=0, help='IP address to run this plugin, Null to local.')
#     parser.add_argument("-u", type=str, default=0, help='user name of the system.')
#     parser.add_argument("-p", type=str, default=0, help='user password of the system.')
#     parser.add_argument("-c", type=int, default=0, help='command to run the function.')
#     parser.add_argument("-n", type=str, default="", help='current username.')
#     parser.add_argument("-v", type=str, default="", help='operation system version, e.g."CentOS 7".')
#     args = parser.parse_args()
#     check(ip=args.i, system_user=args.u, system_pwd=args.p, command=args.c, current_username=args.n,
#           system_version=args.v)
